package com.iwhalecloud.uncc.dao.impl;

import com.iwhalecloud.uncc.common.ArchiverConstant;
import com.iwhalecloud.uncc.common.ArchiverThreadLocal;
import com.iwhalecloud.uncc.dao.ArchiverDao;
import com.iwhalecloud.uncc.db.DatasourceContextHolder;
import com.iwhalecloud.uncc.domain.ArchiveParam;
import com.iwhalecloud.uncc.model.InsEventRecord;
import com.iwhalecloud.uncc.utils.BatchUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author zq
 * @date 2021/10/26 16:26
 * @description InsEventRecordArchiverDaoImpl todo 需要优化SQL
 */
@Repository("insEventRecordArchiverDao")
public class InsEventRecordArchiverDaoImpl implements ArchiverDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Object query(Object o) {
        return null;
    }

    @Override
    public List queryList(Object o) {
        StringBuilder sql = new StringBuilder("SELECT * FROM INS_EVENT_RECORD WHERE OBJECT_ID = ?");
        List<InsEventRecord> list = jdbcTemplate.query(sql.toString(), new Object[]{o} , new BeanPropertyRowMapper<>(InsEventRecord.class));
        if (!list.isEmpty()) {
            return list;
        }
        return null;
    }

    @Override
    public List batchQuery(List list) {
        StringBuilder sql = new StringBuilder("SELECT");
        sql.append(" `EVENT_ID`, `EVENT_TYPE`, `OBJECT_TYPE`, `OBJECT_ID`, `EVENT_LEVEL`, `EVENT_CODE`, `PARAM_INFO`, `EVENT_TIME`, `EVENT_STATE`, `OPERATOR`, `OPER_TIME`, `PARTITION_CODE`");
        sql.append(" FROM INS_EVENT_RECORD WHERE OBJECT_ID in (:i)");
        ArchiveParam aP = ArchiverThreadLocal.getArchiveParam();
        sql.append(" AND EVENT_TIME BETWEEN \"" + aP.getStartDate() + "\" AND \"" + aP.getEndDate()+"\"");
        sql.append(" AND ARCHIVER_FLAG = " + ArchiverConstant.ONLINE);
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        sql.append(" LIMIT " + aP.getNumber());
        List<InsEventRecord> res = BatchUtil.batchQueryBeans(sql,list,new RowMapper() {
            @Override
            public Object mapRow(ResultSet resultSet, int i) throws SQLException {
                InsEventRecord insEventRecord = new InsEventRecord();
                insEventRecord.setEventId(resultSet.getLong("EVENT_ID"));
                insEventRecord.setEventType(resultSet.getInt("EVENT_TYPE"));
                insEventRecord.setEventLevel(resultSet.getInt("EVENT_LEVEL"));
                insEventRecord.setObjectType(resultSet.getInt("OBJECT_TYPE"));
                insEventRecord.setObjectId(resultSet.getString("OBJECT_ID"));
                insEventRecord.setParamInfo(resultSet.getString("PARAM_INFO"));
                insEventRecord.setEventCode(resultSet.getString("EVENT_CODE"));
                insEventRecord.setEventTime(resultSet.getTimestamp("EVENT_TIME"));
                insEventRecord.setEventState(resultSet.getString("EVENT_STATE"));
                insEventRecord.setOperator(resultSet.getString("OPERATOR"));
                insEventRecord.setOperTime(resultSet.getTimestamp("OPER_TIME"));
                insEventRecord.setPartitionCode(resultSet.getString("PARTITION_CODE"));
                return insEventRecord;
            }
        });
        return res;
    }

    @Override
    public void insert(Object o) {

    }

    @Override
    public void batchInsert(List list) {
        StringBuilder sql = new StringBuilder("INSERT INTO `ins_event_record`");
        sql.append(" (`EVENT_ID`, `EVENT_TYPE`, `OBJECT_TYPE`, `OBJECT_ID`, `EVENT_LEVEL`, `EVENT_CODE`, `PARAM_INFO`, `EVENT_TIME`, `EVENT_STATE`, `OPERATOR`, `OPER_TIME`, `PARTITION_CODE`, `ARCHIVER_TIME`)");
        sql.append(" VALUES (:eventId,:eventType,:objectType,:objectId,:eventLevel,:eventCode,:paramInfo,:eventTime,:eventState,:operator,:operTime,:partitionCode,:archiverTime)");
        BatchUtil.batchUpdateBeans(sql,list);
    }

    @Override
    public void update(int flag, Object o) {

    }

    @Override
    public void batchUpdate(int flag, List list) {
        StringBuilder sql = new StringBuilder("UPDATE INS_EVENT_RECORD SET ARCHIVER_FLAG = " + flag + " WHERE OBJECT_ID = :objectId");
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        BatchUtil.batchUpdateBeans(sql,list);
    }

    @Override
    public void delete(Object o) {

    }

    @Override
    public void batchDelete(List list) {
        StringBuilder sql = new StringBuilder("DELETE FROM INS_EVENT_RECORD WHERE EVENT_ID = :eventId");
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        BatchUtil.batchUpdateBeans(sql,list);
    }
}
